<template>
	<view class="container">
		<view class="search_box u-margin-bottom-20">
			<u-search placeholder="请输入员工ID" @custom="searchHandle" v-model="keyword"></u-search>
		</view>
		
		<view class="content_box">
			<view v-if="Object.keys(workerData).length > 0" class="user-item white-bg z-b-r u-margin-bottom-20 u-padding-20 z-font-lg u-flex">
				<view class="u-flex-1">
					<view class="">
						<text>姓名：</text>
						<text>{{ workerData.name }}({{ workerData.sex }})</text>
					</view>
					<view class="">
						<text>联系电话：</text>
						<text>{{workerData.phone}}</text>
					</view>
					<view class="">
						<text>家庭住址：</text>
						<text>{{workerData.address}} {{ workerData.detailAddre }}</text>
					</view>
				</view>
				<view>
					<u-button :disabled="isBind" throttle-time="500" type="primary" @click="bindWorkerHandle(workerData)" size="mini">绑定员工</u-button>
				</view>
			</view>
			<view v-else class="">
				<empty-data tipText="选择您的员工吧"></empty-data>
			</view>
		</view>
		
		<bottom-bar>
			<u-button type="primary" @click="selectAllWK">查看所有员工</u-button>
		</bottom-bar>
		
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {mapState} from "vuex";
	export default {
		data() {
			return {
				keyword:"",
				isSearch:false,
				isBind:false,
				workerData:{}
			}
		},
		computed:{
			...mapState({
				token: "token",
				wkID:"wkID",
				pm_company:"pm_company"
			})
		},
		methods: {
			selectAllWK(){
				uni.navigateTo({
					url:"./childPage/already-bind-wk/already-bind-wk"
				})
			},
			async searchHandle(e){
				if(this.isSearch){
					return false;
				}
				
				this.$loading("正在查找")
				this.isSearch = true;
				let res = await this.$network({
					api:"main",
					data:{
						action:"user/getUserInfoByWkID",
						data:{
							wkID:this.keyword,
							token:this.token
						}
					}
				})
				uni.hideLoading()
				this.isSearch = false;
				if(res.status === 0){
					this.workerData = res.data[0];
				} else {
					this.$refs.uToast.show({
						title: '没有该员工',
						type: 'warning'
					})
				}
			},
			async bindWorkerHandle(){
				let { wkID,token,pm_company,keyword } = this;
				
				if(!pm_company){
					this.$refs.uToast.show({
						title: "您不是项目经理",
						type: 'warning'
					})
					return false;
				}
				
				this.isBind = true;
				this.$loading("正在绑定")
				let res = await this.$network({
					api:"main",
					data:{
						action:"user/pmBindWorker",
						data:{
							wkID:keyword,
							pmID:wkID,
							token,
							pm_company
						}
					}
				})
				
				uni.hideLoading();
				this.isBind = false;
				if(res.status === 0){
					this.$refs.uToast.show({
						title: '绑定成功',
						type: 'success'
					})
				} else if(res.status === 1){
					this.$refs.uToast.show({
						title: "请勿重复操作",
						type: 'warning'
					})
				}
			}
		}
	}
</script>

<style>

</style>
